/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System.Collections.Generic;
using System.IO;
using NMock2;
using NUnit.Framework;
using OpenServers.Net.Common.FileSystem;
using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    public class MockNameListCommand : NameListCommand {
        private readonly IList<object[]> _sendDataCalls = new List<object[]>();

        public IList<object[]> SendDataCalls {
            get { return _sendDataCalls; }
        }

        protected override void SendData(IFtpServer server, FtpState state, Stream dataStream) {
            SendDataCalls.Add(new object[] { server, state, dataStream });
        }
    }

    [TestFixture]
    public class NameListCommandTest : AbstractCommandTest<MockNameListCommand> {

        [Test]
        public void TestInvokeWithParam() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            IList<IFileSystemObject> dirItems = new List<IFileSystemObject>();
            IFile file1 = Mocks.NewMock<IFile>();
            Expect.AtLeastOnce.On(file1).GetProperty("Name").Will(Return.Value("file1"));
            dirItems.Add(file1);
            Expect.Once.On(dir).Method("GetItems").Will(Return.Value(dirItems));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/").Will(Return.Value(dir));
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockSecurityProvider).Method("CanRead").With(MockUser, dir).Will(Return.Value(true));
            Expect.Once.On(MockMessages).Method("Get150").With("/");

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "/");

            Assert.AreEqual("/", State.CurrentDirectory);
            Assert.AreEqual(1, PrivateObject.WrappedObject.SendDataCalls.Count);
        }

        [Test]
        public void TestInvokeWithoutParam() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            IList<IFileSystemObject> dirItems = new List<IFileSystemObject>();
            Expect.Once.On(dir).Method("GetItems").Will(Return.Value(dirItems));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/").Will(Return.Value(dir));
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockSecurityProvider).Method("CanRead").With(MockUser, dir).Will(Return.Value(true));
            Expect.Once.On(MockMessages).Method("Get150").With("/");

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "");

            Assert.AreEqual("/", State.CurrentDirectory);
            Assert.AreEqual(1, PrivateObject.WrappedObject.SendDataCalls.Count);
        }

        [Test]
        public void TestInvokeBadAccess() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/").Will(Return.Value(dir));
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockSecurityProvider).Method("CanRead").With(MockUser, dir).Will(Return.Value(false));
            Expect.Once.On(MockMessages).Method("Get550").With(Reason550.AccessDenied);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "/");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeDirectoryNotFound() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(false));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/").Will(Return.Value(dir));
            Expect.Once.On(MockMessages).Method("Get550").With(Reason550.FileNotFound);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "/");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeNotLoggedIn() {
            State.User = null;
            Expect.Once.On(MockMessages).Method("Get530");
            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "/");
        }
    }
}
